package com.infoland.afanmall.model.my.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.dialog.OpenIsOk_2_Dialog;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.UserKit;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.my.entity.RegisResponse;
import com.vondear.rxtools.RxEncryptTool;
import com.vondear.rxtools.RxRegTool;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.refactor.library.SmoothCheckBox;
import okhttp3.Call;

public class PhoneRegisActivity extends AppCompatActivity {

    @BindView(R.id.regis_back)
    LinearLayout regisBack;
    @BindView(R.id.regis_1_txt1)
    EditText regis1Txt1;
    @BindView(R.id.regis_1_yaznheng)
    LinearLayout regis1Yaznheng;
    @BindView(R.id.regis_1_txt2)
    EditText regis1Txt2;
    @BindView(R.id.regis_1_txt3)
    EditText regis1Txt3;
    @BindView(R.id.regis_1_bt)
    LinearLayout regis1Bt;
    @BindView(R.id.regis_1_check)
    SmoothCheckBox regis1Check;
    @BindView(R.id.zc_texx_yan_zm)
    TextView zcTexxYanZm;

    String phone;
    String yzMa;
    String password;
    boolean isCheck = false;

    //     1 注册 2 登陆 3 忘记密码 4 修改手机号码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhu_ce_phone_activity);
        ButterKnife.bind(this);
        regis1Txt1.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        regis1Txt2.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        regis1Check.setChecked(true);
        initData();
    }

    private void initData() {
        /***获取验证码**/
        regis1Yaznheng.setOnClickListener(v -> {
            phone = regis1Txt1.getText().toString();
            boolean mobile = RxRegTool.isMobile(phone);
            if (mobile) {
                sendYanZM();
            } else {
                ToastUtils.showShort("请输入正确手机号");
            }
        });
        regis1Bt.setOnClickListener(v -> {
            phone = regis1Txt1.getText().toString();
            yzMa = regis1Txt2.getText().toString();
            password = regis1Txt3.getText().toString();
            if (ToolsKit.isPhone(phone)) {
                if (ToolsKit.isYzM(yzMa)) {
                    if (ToolsKit.isPassword(password)) {
                        sendRegis();
                    } else {
                        ToastUtils.showShort("密码长度不可少于6位");
                    }
                } else {
                    ToastUtils.showShort("验证码错误");
                }
            } else {
                ToastUtils.showShort("您的手机号码填写错误");
            }
        });
        regisBack.setOnClickListener(v -> {
            finish();
        });
    }

    /***发送验证码*/
    private void sendYanZM() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phone", phone);
        jsonObject.put("type", 1);
        new OkHttpKit().send(
                response200 -> {
                    JSONObject jsonObject1 = JSONObject.parseObject(response200);
                    if (jsonObject1.getBoolean("success")) {

                    } else {
                        ToolsKit.daoJishi(regis1Yaznheng, zcTexxYanZm, 120 * 1000, 1000, "再次获取");
                        ToastUtils.showShort("验证码发送频繁，请稍后再试");
                    }
                },
                success401 -> {
                    LogKit.E("success401 ---->   ", "  " + success401);
                },
                error -> {
                    LogKit.E("error ---->   ", "  " + error);
                }
        ).post(CONST.SEND_VALIDATE_CODE, jsonObject, this);
        ToolsKit.daoJishi(regis1Yaznheng, zcTexxYanZm, 120 * 1000, 1000, "再次获取");
        ToastUtils.showShort("验证码已发送");
    }

    /***开始注册*/
    private void sendRegis() {
        isCheck = regis1Check.isChecked();
        if (isCheck) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("phone", phone);
            jsonObject.put("password", RxEncryptTool.encryptSHA1ToString(password));
            jsonObject.put("code", yzMa);
            new OkHttpKit().send(
                    response200 -> {
                        LogKit.E("  response ", "  " + response200);
                        RegisResponse regisResponse = JSONObject.parseObject(response200, RegisResponse.class);
                        RegisResponse.ObjectBean object = regisResponse.getObject();
                        if (regisResponse.isSuccess()) {
                            UserKit.putAccessToken(object.getAccess_token());
                            UserKit.putRefreshToken(object.getAccess_token());
                            ToastUtils.showShort("注册成功");
                            finish();
                        } else {
                            OpenIsOk_2_Dialog openDialog = new OpenIsOk_2_Dialog(PhoneRegisActivity.this);
                            openDialog.getOpen_txt1().setText(regisResponse.getMessage() + "");
                            openDialog.getOpen_bt1().setText("取消");
                            openDialog.getOpen_bt2().setText("直接登录");
                            openDialog.getOpen_bt1().setOnClickListener(v -> {
                                openDialog.dismiss();
                            });
                            openDialog.getOpen_bt2().setOnClickListener(v -> {
                                ToolsKit.go(PhoneRegisActivity.this, MsgActivity.class, null);
                                finish();
                            });
                            openDialog.show();
                        }
                    },
                    success401 -> {
                    },
                    error -> {
                        LogKit.E("error ---->   ", "  " + error);
                    }
            ).post(CONST.USER_REGIST, jsonObject, this);
        } else {
            ToastUtils.showShort("同意协议后注册");
        }

    }

    @Override
    public void onDestroy() {
        ToolsKit.deleteOKHttp(this);
        super.onDestroy();
    }

}
